package com.moecollege.umiao;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;

import com.facebook.common.executors.UiThreadExecutorService;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.BaseDataSubscriber;
import com.facebook.datasource.DataSource;
import com.facebook.datasource.DataSubscriber;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.image.CloseableBitmap;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.request.ImageRequest;
import com.moecollege.moecollegelib.widget.MoeImageView;

public class MainActivity extends Activity {
    private MoeImageView image;
    private AppBarLayout barLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (MoeImageView) findViewById(R.id.image);
        barLayout = (AppBarLayout) findViewById(R.id.barlayout);
        Uri barUri = Uri.parse("http://bbs.unpcn.com/attachment.aspx?attachmentid=4224552");
        ImageRequest request = ImageRequest.fromUri(barUri);
        final ImagePipeline line = Fresco.getImagePipeline();
        DataSource<CloseableReference<CloseableImage>> dataSource = line.fetchDecodedImage(request, this);
        DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
            @Override
            protected void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
                if (dataSource == null) {
                    return;
                }
                try {
                    CloseableReference<CloseableImage> reference = dataSource.getResult();
                    Bitmap bitmap = ((CloseableBitmap) reference.get()).getUnderlyingBitmap();
                    Drawable d = new BitmapDrawable(getResources(), bitmap);
                    barLayout.setBackground(d);
                } finally {
                    dataSource.close();
                }
            }

            @Override
            protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {

            }
        };
        dataSource.subscribe(dataSubscriber, UiThreadExecutorService.getInstance());
        Uri uri = Uri.parse("http://jbox.jd.com/preview/download.html?fileId=m6j353nntx3w4&ownerId=YWXPQ33K4H2S4&type=1&opt=preview");
        image.setImageURI(uri);

        if (Build.VERSION.SDK_INT > 19) {
        }

    }

}
